package org.txazo.stat.util;

import java.io.UnsupportedEncodingException;

/**
 * 字符串工具类
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 * 
 */
public class StringUtil {

	/**
	 * 判断字符串是否为空
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	/**
	 * 判断字符串是否不为空
	 */
	public static boolean isNotEmpty(String str) {
		return !StringUtil.isEmpty(str);
	}

	/**
	 * 判断字符串编码格式
	 */
	public static String getEncoding(String str) {
		String encode = null;

		if (StringUtil.isNotEmpty(str)) {
			try {
				if (str.equals(new String(str
						.getBytes(ConstantsUtil.CHARACTER_ENCODING_UTF_8),
						ConstantsUtil.CHARACTER_ENCODING_UTF_8))) {
					encode = ConstantsUtil.CHARACTER_ENCODING_UTF_8;
				} else if (str.equals(new String(str
						.getBytes(ConstantsUtil.CHARACTER_ENCODING_GBK),
						ConstantsUtil.CHARACTER_ENCODING_GBK))) {
					encode = ConstantsUtil.CHARACTER_ENCODING_GBK;
				} else if (str.equals(new String(str
						.getBytes(ConstantsUtil.CHARACTER_ENCODING_GB2312),
						ConstantsUtil.CHARACTER_ENCODING_GB2312))) {
					encode = ConstantsUtil.CHARACTER_ENCODING_GB2312;
				} else if (str.equals(new String(str
						.getBytes(ConstantsUtil.CHARACTER_ENCODING_ISO_8859_1),
						ConstantsUtil.CHARACTER_ENCODING_ISO_8859_1))) {
					encode = ConstantsUtil.CHARACTER_ENCODING_ISO_8859_1;
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return encode;
	}

	/**
	 * 过滤类型
	 */
	public static boolean filterTypes(String type, String[] types) {
		if (StringUtil.isNotEmpty(type)) {
			for (String t : types) {
				if (type.equals(t)) {
					return true;
				}
			}
		}
		return false;
	}

}
